#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 10
#define MAX 100

int Getnum_infile(FILE* DataFile,int* a1)
{
    return fscanf(DataFile,"%d",a1)!=EOF;
}

FILE* MergeSortInFile(FILE* DataFile1,FILE* DataFile2)
{
    FILE *returnFile = fopen(".\\sort_int\\ReturnSort.txt","w");
    int a1 ,a2,flag=1;
    int flaga = 0;
    int flagb = 0;
    while(flag)
    {
        if(flaga == 0)//上次没读取到数字
        {
            flaga = Getnum_infile(DataFile1,&a1);
        }
        if(flagb ==0)//上次没有读取到数字
        {
            flagb = Getnum_infile(DataFile2,&a2);
        }
        flag = flaga+flagb;
        if(flag ==2)
        {
            if(a1>a2)
            {
                fprintf(returnFile,"%d\n",a1);
                flaga = 0;
            }
            else
            {
                fprintf(returnFile,"%d\n",a2);
                flagb = 0;
            }
        }
        else if(flaga==1)
        {
            fprintf(returnFile,"%d\n",a1);
                flaga = 0;
        }
        else if(flagb ==1)
        {
            fprintf(returnFile,"%d\n",a2);
            flagb = 0;
        }
    }

    return returnFile;
}//记住用rename函数改名字，并且删除DataFile1和Dataile2


int cmp(const void* e1,const void* e2)
{
    return *(int*)e1 - *(int*)e2;
}
void CreatFileSort(int* arr,int arrSize,int* file_countPointer)
{
        if(arrSize<=0)
        {
            return ;
        }
        int  i = arrSize;
        int file_count =*file_countPointer;
        char sort_Filename[50] = {0};
        qsort(arr,N,sizeof(int),&cmp);
        sprintf(sort_Filename,".\\sort_int\\sort_Filename_%d",file_count);
        FILE* tmp_filepointer = fopen(sort_Filename,"w");
        if(!tmp_filepointer)
        {
            perror("");
            return;
        }
        while(i>0)
        {
            fprintf(tmp_filepointer,"%d ",arr[--i]);
        }
        fclose(tmp_filepointer);
        file_count++;
        *file_countPointer = file_count;
}



void Test_1()
{
    srand(time(NULL));//写入乱的数据
    const char* Fsortname = ".\\sort_int\\AllSort.txt";
    FILE* Fsort = fopen(Fsortname,"w");
    if(!Fsort)
    {
        perror("");
    }
    for(int i = 0;i<MAX;i++)
    {
        fprintf(Fsort,"%d ",rand()%MAX);
    }
    fclose(Fsort);


    Fsort = fopen(Fsortname,"r");//先分成小的文件，创建小的排序文件
    int single_Data = 0;
    int i=0;
    int arr[N] = {0};
    int file_count = 0;
    while(fscanf(Fsort,"%d",&single_Data)!=EOF)
    {
        arr[i++] = single_Data;
        if(i==N)
        {
            CreatFileSort(arr,i,&file_count);
            i = 0;
        }
    }
    CreatFileSort(arr,i,&file_count);
    fclose(Fsort);//至此排序文件创建完毕



    while(file_count>1)//开始在文件里面排序
    {
        int tcount = 0;
        int a = 0;
        while(a<file_count)
        {
            char sort_Filename[50] = {0};
            sprintf(sort_Filename,".\\sort_int\\sort_Filename_%d",a++);
            FILE* tmp1_filepointer = fopen(sort_Filename,"r");
            if(!tmp1_filepointer)
            {
                perror("");
                return;
            }
            if(a<file_count)
            {
                sprintf(sort_Filename,".\\sort_int\\sort_Filename_%d",a++);
                FILE* tmp2_filepointer = fopen(sort_Filename,"r");
                if(!tmp2_filepointer)
                {
                    perror("");
                    return;
                }
                FILE* retmp = MergeSortInFile(tmp1_filepointer,tmp2_filepointer);
                fclose(tmp1_filepointer);
                fclose(tmp2_filepointer);
                remove(sort_Filename);
                sprintf(sort_Filename,".\\sort_int\\sort_Filename_%d",a-2);
                remove(sort_Filename);
                sprintf(sort_Filename,".\\sort_int\\sort_Filename_%d",tcount++);
                fclose(retmp);
                rename(".\\sort_int\\ReturnSort.txt",sort_Filename);
            }
            else
            {
                char sort_Filename2[50] = {0};
                sprintf(sort_Filename2,".\\sort_int\\sort_Filename_%d",tcount-1);
                FILE* tmp2_filepointer = fopen(sort_Filename2,"r");
                if(!tmp2_filepointer)
                {
                    perror("");
                    return;
                }
                FILE* retmp = MergeSortInFile(tmp1_filepointer,tmp2_filepointer);
                fclose(tmp1_filepointer);
                fclose(tmp2_filepointer);
                remove(sort_Filename);
                remove(sort_Filename2);
                fclose(retmp);
                rename(".\\sort_int\\ReturnSort.txt",sort_Filename2);
            }
            
        }
        file_count = tcount;
    }
    //排序结束
    rename(".\\sort_int\\sort_Filename_0",".\\sort_int\\returnSort.txt");

    return ;
}

int main()
{
    Test_1();
    return 0;
}